import { Global, Module } from "@nestjs/common";
import { AuthService } from "./auth.service";
import { AuthController } from "./auth.controller";
import { UserModule } from "../user/user.module";
import { PassportModule } from "@nestjs/passport";
import { JwtModule } from "@nestjs/jwt";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { ConfigEnum } from "src/enum/config.enum";
import { JwtStrategy } from "./jwt.strategy";
import { CaslAbilityService } from "./casl-ability.service";
import { RedisModule } from "../redis/redis.module";
import { JwtGuard } from "../guards/jwt.guard";

@Global()
@Module({
  imports: [
    UserModule,
    PassportModule,
    RedisModule, // 导入 Redis 模块
    JwtModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => {
        // 从配置中获取过期时间（秒）
        const expiresInSeconds = configService.get<number>(ConfigEnum.JWT_EXPIRES_IN, 86400);

        return {
          secret: configService.get<string>(ConfigEnum.SECRET),
          signOptions: {
            // 直接使用数字（秒数），JWT 模块接受数字类型
            expiresIn: expiresInSeconds,
          },
        };
      },
      inject: [ConfigService],
    }),
  ],
  providers: [AuthService, JwtStrategy, CaslAbilityService, JwtGuard], // 将 JwtGuard 添加为 provider
  controllers: [AuthController],
  exports: [CaslAbilityService, AuthService, JwtGuard], // 导出 AuthService 和 JwtGuard
})
export class AuthModule {}
